package com.zxl.live.tools.anim;

import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/6/23.
 */

public class ExpandAnimation extends Animation {

    private View mView;

    private int mHeight;

    public ExpandAnimation(View mView) {
        this.mView = mView;
        this.mHeight = getViewHeight(mView);
    }

    public ExpandAnimation(View mView, int mHeight) {
        this.mView = mView;
        this.mHeight = mHeight;
    }

    @Override
    protected final void applyTransformation(float paramFloat, Transformation paramTransformation) {
        ViewGroup.LayoutParams localLayoutParams = mView.getLayoutParams();
        localLayoutParams.height = (int) (paramFloat * mHeight);
        mView.requestLayout();
    }

    @Override
    public final boolean willChangeBounds() {
        return true;
    }


    public static int getViewHeight(View view) {
        try {
            Class localClass = view.getClass();
            Class[] arrayOfClass = new Class[2];
            arrayOfClass[0] = Integer.TYPE;
            arrayOfClass[1] = Integer.TYPE;
            Method localMethod = localClass.getDeclaredMethod("onMeasure", arrayOfClass);
            localMethod.setAccessible(true);
            Object[] arrayOfObject = new Object[2];
            arrayOfObject[0] = Integer.valueOf(View.MeasureSpec.makeMeasureSpec(((View) view.getParent()).getMeasuredWidth(), Integer.MIN_VALUE));
            arrayOfObject[1] = Integer.valueOf(View.MeasureSpec.makeMeasureSpec(0, 0));
            localMethod.invoke(view, arrayOfObject);
            return view.getMeasuredHeight();
        } catch (Exception localException) {
            for (; ; ) {
            }
        }
    }

}
